package com.hspedu.homework;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/2/5
 * @time 17:40
 */
public class Homework01 {
    public static void main(String[] args) {

        String str = "abcde";
        try {
            reverse(str,1,2);
            reverse2(str,1,2);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public static String reverse(String str,int start,int end){

        if (str != null && start >= 0 && end < str.length() && end > start){
            char[] chars = str.toCharArray();
            char[] newChars = new char[end - start + 1];
            for (int i = end , j = 0 ; i >= start ; i-- , j++){
                newChars[j] = chars[i];
            }
            for (int i = start , j = 0; i <= end ; i++ , j++) {
                chars[i] = newChars[j];
            }
            return new String(chars);
        }else {
            throw new RuntimeException("参数不正确");
        }
    }

    public static String reverse2(String str,int start,int end) {

        if (str != null && start >= 0 && end < str.length() && end > start) {
            char[] chars = str.toCharArray();
            char temp = 0;
            for (int i = start, j = end; i < j; i++, j--) {
                temp = chars[i];
                chars[i] = chars[j];
                chars[j] = temp;
            }

            return new String(chars);
        } else {
            throw new RuntimeException("参数不正确");
        }
    }
}
